/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.table.explorer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import twaver.Element;
import twaver.TDataBox;
import twaver.table.TTable;
import twaver.table.TTablePopupMenuFactory;

public class PopupMenuFactory implements TTablePopupMenuFactory{
	
	private TDataBox box;
	public PopupMenuFactory(TDataBox box){
		this.box = box;
	}
	
	public JPopupMenu getPopupMenu(TTable table, MouseEvent e) { 
		if(box.getSelectionModel().size() == 1){
			final Element element = box.getLastSelectedElement();

			JPopupMenu popup = new JPopupMenu();
			JMenuItem moveTop = new JMenuItem("Move Top");
			JMenuItem moveUp = new JMenuItem("Move Up");
			JMenuItem moveDown = new JMenuItem("Move Down");
			JMenuItem moveBottom = new JMenuItem("move Bottom");
			popup.add(moveTop);
			popup.add(moveUp);
			popup.add(moveDown);
			popup.add(moveBottom);
			
			moveTop.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					box.moveToTop(element);
				}
			});
			moveUp.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					box.moveToUp(element);
				}
			});
			moveDown.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					box.moveToDown(element);
				}
			});
			moveBottom.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					box.moveToBottom(element);
				}
			});
			
			return popup;
		}
		return null;
	}
}